package team.seekers.skill.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import team.seekers.skill.entity.dao.SkillComment;
import team.seekers.skill.entity.query.CommentQuery;
import team.seekers.skill.entity.vo.fore.add.SkillCommentAddVO;
import team.seekers.skill.entity.vo.fore.update.SkillCommentUpdateVO;
import team.seekers.skill.result.Result;
import team.seekers.skill.service.SkillCommentService;
import team.seekers.skill.common.utils.ParamCheckUtils;
import team.seekers.skill.common.utils.SkillUtils;

import java.util.List;
import java.util.Map;

import static team.seekers.skill.result.ResultCode.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbc
 * @since 2021-01-18
 */
@RestController
@RequestMapping("/api/skill/comment")
@Validated
public class CommentController {

    @Autowired
    private SkillCommentService skillCommentService;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result add(@RequestBody @Validated SkillCommentAddVO commentVO){
        QueryWrapper<SkillComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",SkillUtils.getUserId());
        List<SkillComment> skillCommentList = skillCommentService.list(wrapper);
        if(skillCommentList.size() != 0){
            return Result.fail(ACTION_FAIL).message("请勿重复评论");
        }
        boolean add = skillCommentService.add(commentVO);
        if (!add){
            return Result.fail(ACTION_FAIL).message("添加失败，请稍后重试");
        }
        return Result.success();
    }

    @DeleteMapping("/delete/{commentId}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result delete(@PathVariable("commentId")String commentId){
        if (ParamCheckUtils.isNotRightId(commentId)){
            return Result.fail(PARAM_NOT_VALID);
        }
        boolean delete = skillCommentService.delete(commentId);
        if(!delete){
            return Result.fail(ACTION_FAIL).message("删除失败，请稍后重试");
        }
        return Result.success();
    }

    @PostMapping("/update")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result update(@RequestBody @Validated SkillCommentUpdateVO commentVO){
        if (ParamCheckUtils.isNotRightId(commentVO.getId())){
            return Result.fail(PARAM_NOT_VALID);
        }
        boolean update = skillCommentService.update(commentVO);
        if(!update){
            return Result.fail(ACTION_FAIL).message("更新失败，请稍后重试");
        }
        return Result.success();
    }

    //分页查询
    @PostMapping("/pageCommentCondition/{current}/{limit}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result pageCommentCondition(@PathVariable("current") long current,
                                       @PathVariable("limit") long limit,
                                       @RequestBody CommentQuery commentQuery) {
        if(current<1 || limit<1){
            return Result.fail(MAIN_GET_FAIL);
        }
        if (ParamCheckUtils.isNotRightId(commentQuery.getSkillId())){
            return Result.fail(PARAM_NOT_VALID);
        }
        Map<String, Object> map = skillCommentService.pageCommentCondition(current, limit, commentQuery);
        if (map == null){
            return Result.fail(GET_FAIL);
        }
        //这里返回模糊查询得到的总数量和所有的纪录
        return Result.success(map);
    }
}

